#!/usr/bin/python
# encoding=utf-8
import cv
import math
import os
import pickle
import sys
import math
import heapq
import numpy as np


################################################################################
# Handle Command Line Args
################################################################################

if len(sys.argv) < 2:
    print "Usage: info.pickle graph.pickle face1 face2 depth frames working-dir movie"
    exit(1)

faces_path=sys.argv[1];
frames=int(sys.argv[2]);
workingdir=sys.argv[3];
movieFilePath = sys.argv[4];

# #load the graph
# graphFile = open(graphFilePath,"r")
# graph = pickle.load(graphFile)
# graphFile.close()

#load the info
# infoFile = open(infoFilePath,"r")
# info = pickle.load(infoFile)
# infoFile.close()

def crossfade(src1,src2,output,percent):
    """Creates a cross faded image given a specific percent"""
    command = "composite -dissolve "+str(percent)+"% "+src2+" "+src1+"  "+output+" "
    print command
    os.system(command)

def fadeSequence(src1,src2,n,workingDir,startIndex):
    """Creates a series of cross faded images starting naming with startIndex"""
    for i in range(n):
        crossfade(src1,src2,"%s%04d.png"%(workingDir,startIndex+i),i*100.0/(n-1))

def fadeList(srcs,n,workingDir):
    index = 0
    for i in range(len(srcs)-1):
        print "s:",srcs[i]
        print "t:", srcs[i+1]
        fadeSequence(srcs[i],srcs[i+1],n,workingDir,index)
        index += n
faces=[]
for face_path in os.listdir(faces_path):
    face=faces_path+face_path
    faces.append(face)
    print face
print "face_len:",len(faces)
print "faces:",faces
fadeList(faces,frames,workingdir)
os.system("ffmpeg -i "+workingdir+"%04d.png  -vcodec mpeg4  "+movieFilePath+"")

